package edu.megatrend.fkn.rsw.ocenjivanje.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import edu.megatrend.fkn.rsw.ocenjivanje.beans.Student;
import edu.megatrend.fkn.rsw.ocenjivanje.logika.UnosStudenata;

/**
 * Servlet implementation class UnosStudenta
 */
public class UnosStudenta extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UnosStudenta() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		// HttpSession sesija = request.getSession();
		Student student = new Student();// (Student)
										// sesija.getAttribute("student");

		try {
			out.println("<html>");
			out.println("<head>");
			out.println("<title>Unos studenata</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<form action=\"addStud\" method=\"post\" >");
			out.println("<table width=\"35%\" border=\"3\">"
					+ "<tr><td width=\"40%\">Indeks:</td>"
					+ "<td><input type=\"text\" name=\"indeks\" value=\""
					+ student.getIndeks() + "\"/></td></tr>");
			out.println("<tr><td>Ime:</td>"
					+ "<td><input type=\"text\" name=\"ime\" value=\""
					+ student.getIme() + "\"/></td></tr>");
			out.println("<tr><td>Prezime:</td>"
					+ "<td><input type=\"text\" name=\"prezime\" value=\""
					+ student.getPrezime() + "\"/></td></tr>");
			out.println("<tr><td>Semestar:</td>"
					+ "<td><input type=\"text\" name=\"semestar\" value=\""
					+ student.getSemestar() + "\"/></td></tr>");
			out.println("<tr><td>Godina:</td>"
					+ "<td><input type=\"text\" name=\"datum\" value=\""
					+ student.getGodina() + "\"/></td></tr>");
			out.println("</table><br/>");
			out.println("<input type=\"submit\" value=\"Prihvatite izmenu\"/>");
			out.println("<input type=\"reset\" value=\"Odbacite izmenu\"/>");

			out.println("</form></body>");
			out.println("<a href=\"index\">Nazad</a>");
			out.println("</html>");
		} finally {
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		@SuppressWarnings("rawtypes")
		Enumeration paramNames = request.getParameterNames();
		Properties formValues = new Properties();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					formValues.setProperty(paramName, paramValue);
				}
			}
		}
		
		UnosStudenata t = new UnosStudenata();
		t.dodajStudenta(formValues);

		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
